/**
 * Copyright (C) 2011 Audit Logger Project. All rights reserved.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.auditlogger.config.providers;

import org.auditlogger.config.spi.PluginConfigurator;
import org.auditlogger.config.spi.PropertiesConfigurator;
import org.auditlogger.config.util.Constants;
import org.auditlogger.config.util.NoSuchPropertyException;
import org.auditlogger.config.util.ObjectFactory;

public final class DefaultPluginConfigurator extends PluginConfigurator {
	
    
	private static volatile PropertiesConfigurator staticConfig = null;

	@Override
	public PropertiesConfigurator getStaticConfigurationProvider() {
		if (staticConfig == null) {
			String auditLogFactoryName = 
				PropertiesReader.getValueForPluginProperty(Constants.STATIC_CONF_PROVIDER);
			staticConfig = ObjectFactory.make(auditLogFactoryName, 
					PropertiesConfigurator.class.getName());	
		}
		return staticConfig;
	}

	/**
	 * 
	 * @param pluginProperty java.lang.String plugin property.
	 * @return plugin property value.
	 * @throws IllegalArgumentException If pluginProperty is null or "".
	 * @throws NoSuchPropertyException If pluginProperty is not found in the plugin.config file 
	 */
	public <T> T getPlugin(String pluginProperty) {
		if (pluginProperty == null || pluginProperty.trim().equals("")) {
			throw new IllegalArgumentException ("input pluginproperty is null or empty");
		}
		String propertyValue = PropertiesReader.getValueForPluginProperty(
				pluginProperty);
		if (propertyValue == null) {
			throw new NoSuchPropertyException("Couldn't find " + pluginProperty 
					+ "in plugin.properties");
		}
		return this.getPlugin(propertyValue, propertyValue);
	}
}
